package com.ruoyi.zhgd.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.SysDeptVo;
import com.ruoyi.zhgd.domain.bo.SysDeptBo;
import com.ruoyi.zhgd.service.ISysDeptBeanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 组织
 *
 * @author zyb
 * @date 2023-09-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/dept")
public class SysDeptBeanController extends BaseController {

    private final ISysDeptBeanService iSysDeptBeanService;

    /**
     * 查询组织列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<SysDeptVo> list(SysDeptBo bo, PageQuery pageQuery) {
        return iSysDeptBeanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出组织列表
     */
    @SaCheckLogin
    @Log(title = "组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysDeptBo bo, HttpServletResponse response) {
        List<SysDeptVo> list = iSysDeptBeanService.queryList(bo);
        ExcelUtil.exportExcel(list, "组织", SysDeptVo.class, response);
    }

    /**
     * 获取组织详细信息
     *
     * @param deptId 主键
     */
    @SaCheckLogin
    @GetMapping("/{deptId}")
    public R<SysDeptVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long deptId) {
        return R.ok(iSysDeptBeanService.queryById(deptId));
    }

    /**
     * 新增组织
     */
    @SaCheckLogin
    @Log(title = "组织", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysDeptBo bo) {
        return toAjax(iSysDeptBeanService.insertByBo(bo));
    }

    /**
     * 修改组织
     */
    @SaCheckLogin
    @Log(title = "组织", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDeptBo bo) {
        return toAjax(iSysDeptBeanService.updateByBo(bo));
    }

    /**
     * 删除组织
     *
     * @param deptIds 主键串
     */
    @SaCheckLogin
    @Log(title = "组织", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deptIds) {
        return toAjax(iSysDeptBeanService.deleteWithValidByIds(Arrays.asList(deptIds), true));
    }
}
